package com.two.screen.water.balance.widget.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.TextView
import com.jakewharton.rxbinding2.view.RxView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.listener.ApiCallBack
import com.two.screen.water.balance.net.api.OrderApi
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.ui.activity.base.BaseActivity
import com.two.screen.water.balance.utils.*
import java.util.concurrent.TimeUnit


class OrderUploadDialog(var mContext: Context) : Dialog(mContext, R.style.ChooseDialog) {

    private var mDialogOrderUploadLeftNumTv: TextView? = null
    private var mDialogOrderUploadConfirmTv: TextView? = null
    var mLoadingDialog: LoadingDialog = LoadingDialog(mContext)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_order_upload)
        this.setCanceledOnTouchOutside(false)

        val window = window
        if (window != null) {
            val attributes = window.attributes
            attributes.width = App.screenWidth
            window.attributes = attributes
            window.setGravity(Gravity.CENTER)
        }
        mDialogOrderUploadLeftNumTv = findViewById(R.id.mDialogOrderUploadLeftNumTv)
        mDialogOrderUploadConfirmTv = findViewById(R.id.mDialogOrderUploadConfirmTv)

        findViewById<View>(R.id.mCloseIv).apply {
            setOnClickListener {
                dismiss()
            }
        }

        setNewData()
        mDialogOrderUploadConfirmTv?.let {
            RxView.clicks(it)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe {
//                    mLoadingDialog.show()
//                    mLoadingDialog.mTitle = "正在提交上传..."
                    d("开始提交上传...")
                    OrderApi.batchUploadCashOrder(mContext as BaseActivity, object : ApiCallBack() {
                        override fun success(t: Any?) {
                            setNewData()
                            ToastUtil.showToast(t as String)
//                            mLoadingDialog.dismiss()
                        }

                        override fun fail(t: Throwable) {
//                            mLoadingDialog.dismiss()
                            ToastUtil.showToast(t.message)
                        }
                    })
                }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun setNewData() {
        OrderApi.getNoUploadCashOrderSize(mContext as BaseActivity, object : ApiCallBack() {
            override fun success(t: Any?) {
                var size = t as Int
                mDialogOrderUploadLeftNumTv?.text = "$size 笔"
            }
        })
    }

    fun d(msg: String) {
        CommUtils.println("OrderUploadDialog->", msg)
    }
    override fun show() {
        super.show()
        //隐藏状态栏
        window?.let { DialogUtil.hideNavigationBar(it) }
    }
}
